package Example;

import java.util.Scanner;

//题目：求1+2!+3!+...+20!的和
//或计算前n项的阶乘和
public class Demo19 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("输入项数n：");
        int n = scan.nextInt();
        int t = 1;
        int sum = 0;
        for (int i = 1; i <= n; i++) {//递归方法求阶乘
            t*=i;
            sum+=t;
        }
        System.out.println("前" + n + "项的阶乘和为\n" + sum);
    }
}
/*
*       int sum = 0;
*		int factorial = 1;
*   	for (int i = 1; i <= n; i++) {
*	    	for (int j = 1; j <= i; j++) {
*		    	factorial = factorial * j;
*    		}
*	    	sum += factorial;
*    		factorial = 1;
*    	}
*    	System.out.println("1+2!+..." + n + "!" + "的和为" + sum);
 */
